package red.lixiang.service.impl;

import red.lixiang.dao.MemberDAO;
import red.lixiang.dao.OrderDAO;
import red.lixiang.model.BaseInfo;
import red.lixiang.model.Member;
import red.lixiang.model.Order;
import red.lixiang.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class AccountServiceImpl implements AccountService{

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private MemberDAO memberDAO;

    @Autowired
    private OrderDAO orderDAO;



    @Override
    public BaseInfo submitLoginByVerifyCode(String mobilePhone, String verifyCode,String openId) {

        BaseInfo baseInfo = new BaseInfo("0","OK");


        String redisKey  = "verifyCode:"+mobilePhone;
        String redisVerifyCode = String.valueOf(redisTemplate.opsForValue().get(redisKey));
       // redisVerifyCode=redisVerifyCode.substring(1,redisVerifyCode.length()-1);
        if(redisVerifyCode!=null && verifyCode.equals(redisVerifyCode)){
            //登录验证成功，根据手机号去取会员信息
            Member member = memberDAO.getMemberByMobile(mobilePhone);
            Member member_openId = memberDAO.getMemberByOpenId(openId);
            //如果根据openId查会员是已在的话，则更新手机号
            if(member_openId !=null && member ==null){
                member_openId.setMemberMobile(mobilePhone);
                memberDAO.updateMember(member_openId);
            }
            if (member!=null && member_openId==null){
                //如果会员不为空，则把wxOpenId更新进去
                member.setWxUnionId(openId);
                memberDAO.updateMember(member);
            }
            if(member==null && member_openId==null){
                //如果会员为空的话，则把新建一个会员，手机号和openId填充进去，其他的默认
                member=new Member();
                String cardId = "0"+mobilePhone;
                member.setMemberMobile(mobilePhone);
                member.setMemberName("待添加");
                member.setCardName("德蒙钻石卡");
                member.setMemberDiscount(0.38);
                member.setCardId(cardId);
                memberDAO.addNewMember(member);
                member.setWxUnionId(openId);
                memberDAO.updateMember(member);
            }
            if(member!=null&&member_openId!=null){
                //微信随机会员和手机号会员同时存在。
                //把order表，address表中的会员 id 更改成手机号的id，
                // 然后把微信号的会员从会员表中删除
                member.setWxUnionId(openId);
                memberDAO.updateMember(member);
                int newId = member.getId();
                int oldId = member_openId.getId();
                List<Order> memberOrder = orderDAO.getOrderByMemberId(oldId);
                for (Order order : memberOrder) {
                    order.setMemberId(newId);
                    orderDAO.updateOrder(order);
                }
                memberDAO.deleteMemberById(oldId);
            }

            //把member信息放到baseInfo中,然后在Controller中再存入Session
            baseInfo.setObject(member);
        }

        return baseInfo;
    }
}
